/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.drive.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.basic.common.constant.FlowDicConstant;
import com.hopes.bizz.basic.drive.dto.BaseAuditDTO;
import com.hopes.bizz.basic.drive.dto.BaseOverhaulFlowSearchDTO;
import com.hopes.bizz.basic.drive.dto.BaseOverhaulRecordExcelDTO;
import com.hopes.bizz.basic.drive.entity.BaseOverhaulRecordEntity;
import com.hopes.bizz.basic.drive.service.BaseOverhaulRecordService;
import com.hopes.bizz.basic.drive.vo.BaseOverhaulRecordExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 大检修申报
 *
 * @author pig
 * @date 2023-11-08 16:30:28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/overhaul" )
@Tag(description = "overhaul" , name = "大检修申报管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BaseOverhaulRecordController {

    private final BaseOverhaulRecordService baseOverhaulRecordService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param baseOverhaulRecord 大检修申报
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('drive_overhaul_view')" )
    public R getbaseOverhaulRecordPage(Page<BaseOverhaulRecordEntity> page, BaseOverhaulRecordEntity baseOverhaulRecord) {
        LambdaQueryWrapper<BaseOverhaulRecordEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.ge(ObjectUtil.isNotNull(baseOverhaulRecord.getStartDate()),BaseOverhaulRecordEntity::getStartDate,baseOverhaulRecord.getStartDate());
		wrapper.le(ObjectUtil.isNotNull(baseOverhaulRecord.getStopDate()),BaseOverhaulRecordEntity::getStopDate,baseOverhaulRecord.getStopDate());
		wrapper.eq(StrUtil.isNotBlank(baseOverhaulRecord.getOverhualStatus()),BaseOverhaulRecordEntity::getOverhualStatus,baseOverhaulRecord.getOverhualStatus());
        wrapper.eq(StrUtil.isNotBlank(baseOverhaulRecord.getOrgId()),BaseOverhaulRecordEntity::getOrgId,baseOverhaulRecord.getOrgId());
		wrapper.like(StrUtil.isNotBlank(baseOverhaulRecord.getFacilityName()),BaseOverhaulRecordEntity::getFacilityName,baseOverhaulRecord.getFacilityName());
        wrapper.orderByDesc(BaseOverhaulRecordEntity::getOrgId);
        return R.ok(baseOverhaulRecordService.page(page, wrapper));
    }

    /**
     * 申报界面分页查询
     * @param page 分页对象
     * @param baseOverhaulRecord 大检修申报
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/declarePage" )
    @PreAuthorize("@pms.hasPermission('drive_overhaul_view')" )
    public R getbaseOverhaulDeclarePage(Page<BaseOverhaulRecordEntity> page, BaseOverhaulRecordEntity baseOverhaulRecord) {
        LambdaQueryWrapper<BaseOverhaulRecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(ObjectUtil.isNotNull(baseOverhaulRecord.getStartDate()),BaseOverhaulRecordEntity::getStartDate,baseOverhaulRecord.getStartDate());
        wrapper.le(ObjectUtil.isNotNull(baseOverhaulRecord.getStopDate()),BaseOverhaulRecordEntity::getStopDate,baseOverhaulRecord.getStopDate());
        wrapper.eq(StrUtil.isNotBlank(baseOverhaulRecord.getOverhualStatus()),BaseOverhaulRecordEntity::getOverhualStatus,baseOverhaulRecord.getOverhualStatus());
        wrapper.eq(StrUtil.isNotBlank(baseOverhaulRecord.getOrgId()),BaseOverhaulRecordEntity::getOrgId,baseOverhaulRecord.getOrgId());
        wrapper.like(StrUtil.isNotBlank(baseOverhaulRecord.getFacilityName()),BaseOverhaulRecordEntity::getFacilityName,baseOverhaulRecord.getFacilityName());
        wrapper.and(item->item.eq(BaseOverhaulRecordEntity::getOverhualStatus, FlowDicConstant.NOT_PASS).or().eq(BaseOverhaulRecordEntity::getOverhualStatus, FlowDicConstant.FILL_IN));
        wrapper.orderByDesc(BaseOverhaulRecordEntity::getOrgId);
        return R.ok(baseOverhaulRecordService.page(page, wrapper));
    }



    /**
     * 通过id查询大检修申报
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('drive_overhaul_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(baseOverhaulRecordService.getById(id));
    }

    /**
     * 新增大检修申报
     * @param baseOverhaulRecord 大检修申报
     * @return R
     */
    @Operation(summary = "新增大检修申报" , description = "新增大检修申报" )
    @SysLog("新增大检修申报" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('drive_overhaul_add')" )
    public R save(@RequestBody BaseOverhaulRecordEntity baseOverhaulRecord) {
        baseOverhaulRecord.setOverhualStatus(FlowDicConstant.FILL_IN);
        return R.ok(baseOverhaulRecordService.save(baseOverhaulRecord));
    }

    /**
     * 修改大检修申报
     * @param baseOverhaulRecord 大检修申报
     * @return R
     */
    @Operation(summary = "修改大检修申报" , description = "修改大检修申报" )
    @SysLog("修改大检修申报" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('drive_overhaul_edit')" )
    public R updateById(@RequestBody BaseOverhaulRecordEntity baseOverhaulRecord) {
        return R.ok(baseOverhaulRecordService.updateById(baseOverhaulRecord));
    }

    /**
     * 通过id删除大检修申报
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除大检修申报" , description = "通过id删除大检修申报" )
    @SysLog("通过id删除大检修申报" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('drive_overhaul_del')" )
    public R removeById(@RequestBody Long[] ids) {
        baseOverhaulRecordService.deleteTask(CollUtil.toList(ids));
        return R.ok();
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('drive_overhaul_export')")
    public List<BaseOverhaulRecordExcelVO> template() {
        return baseOverhaulRecordService.exportExcel(Arrays.asList(new BaseOverhaulRecordEntity()));
    }

    /**
     * 导出excel 表格
     * @param baseOverhaulRecordExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('drive_overhaul_export')")
    public List<BaseOverhaulRecordExcelVO> export(BaseOverhaulRecordExcelDTO baseOverhaulRecordExcelDto) {
        LambdaQueryWrapper<BaseOverhaulRecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(ObjectUtil.isNotNull(baseOverhaulRecordExcelDto.getStartDate()),BaseOverhaulRecordEntity::getStartDate,baseOverhaulRecordExcelDto.getStartDate());
        wrapper.le(ObjectUtil.isNotNull(baseOverhaulRecordExcelDto.getStopDate()),BaseOverhaulRecordEntity::getStopDate,baseOverhaulRecordExcelDto.getStopDate());
        wrapper.eq(StrUtil.isNotBlank(baseOverhaulRecordExcelDto.getOverhualStatus()),BaseOverhaulRecordEntity::getOverhualStatus,baseOverhaulRecordExcelDto.getOverhualStatus());
        wrapper.like(StrUtil.isNotBlank(baseOverhaulRecordExcelDto.getFacilityName()),BaseOverhaulRecordEntity::getFacilityName,baseOverhaulRecordExcelDto.getFacilityName());
        return baseOverhaulRecordService.exportExcel(baseOverhaulRecordService.list(wrapper));
    }

    /**
     * 导入excel 表格
     * @param baseOverhaulRecordExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('drive_overhaul_export')")
    public R importExcel(@RequestExcel List<BaseOverhaulRecordExcelDTO> baseOverhaulRecordExcelDtoList, BindingResult bindingResult) {
        return baseOverhaulRecordService.importExcel(baseOverhaulRecordExcelDtoList, bindingResult);
    }

    @Operation(summary = "查询我的待审批", description = "查询我的待审批")
    @GetMapping("/queryTodoForPage")
    public R queryTodoForPage(Page page, BaseOverhaulFlowSearchDTO flowSearch) {
        flowSearch.setTaskKey(FlowDicConstant.IN_REVIEW);
        Page res = baseOverhaulRecordService.queryTodoForPage(page, flowSearch);
        return R.ok(res);
    }

    @Operation(summary = "提交大检修审批结果", description = "提交大检修审批结果")
    @SysLog("提交大检修审批结果")
    @PostMapping("/handlerAudit")
    public R handlerAudit(@Validated @RequestBody BaseAuditDTO baseAuditDTO) {
        baseOverhaulRecordService.handlerAudit(baseAuditDTO);
        return R.ok();
    }

    @Operation(summary = "提交", description = "提交")
    @SysLog("提交")
    @GetMapping("/overhaulSubmit/{id}")
    public R driveSubmit(@PathVariable("id")  Long id) {
        baseOverhaulRecordService.handleOverhaulSubmit(id);
        return R.ok();
    }
}
